package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.RoleMapper;
import com.cqjtu.tto.dao.mapper.SysUserMapper;
import com.cqjtu.tto.dao.mapper.SysUserRoleMapper;
import com.cqjtu.tto.dao.pojo.Role;
import com.cqjtu.tto.dao.pojo.SysUser;
import com.cqjtu.tto.dao.pojo.SysUserRole;
import com.cqjtu.tto.service.RoleService;
import com.cqjtu.tto.service.SysUserRoleService;
import com.cqjtu.tto.service.SysUserService;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.SysUserRoleVo;
import com.cqjtu.tto.vo.params.SysUserRoleParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：monkey-L
 * @description:
 * @date: 20:20 2021/12/11
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public Results sysUserRoleAdd(SysUserRoleParam sysUserRoleParam) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUserName,sysUserRoleParam.getUserName()).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        if (sysUser == null) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),ErrorCode.USER_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,sysUserRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode() ,ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        sysUserRoleWrapper.eq(SysUserRole::getRoleId,role.getRoleId()).eq(SysUserRole::getUserId,sysUser.getUserId()).last("limit 1");
        SysUserRole sysUserRole1 = sysUserRoleMapper.selectOne(sysUserRoleWrapper);
        if (sysUserRole1 != null) {
            return Results.fail(1025,"该用户的此角色已经存在");
        }
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserRoleId(System.currentTimeMillis());
        sysUserRole.setUserRoleDeleted(0);
        sysUserRole.setRoleId(role.getRoleId());
        sysUserRole.setUserId(sysUser.getUserId());
        sysUserRoleMapper.insert(sysUserRole);
        return Results.success("为该用户添加角色成功！");
    }


    @Override
    @Transactional
    public Results sysUserRoleDelete(SysUserRoleParam sysUserRoleParam) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUserName,sysUserRoleParam.getUserName()).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        if (sysUser == null) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),ErrorCode.USER_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,sysUserRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode() ,ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        sysUserRoleWrapper.eq(SysUserRole::getRoleId,role.getRoleId()).eq(SysUserRole::getUserId,sysUser.getUserId()).last("limit 1");
        SysUserRole sysUserRole1 = sysUserRoleMapper.selectOne(sysUserRoleWrapper);
        if (sysUserRole1 == null) {
            return Results.fail(1026,"该用户当前未拥有此角色，无法删除！");
        }
        LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserRoleLambdaQueryWrapper.eq(SysUserRole::getUserRoleId,sysUserRole1.getUserRoleId());
        sysUserRoleMapper.delete(sysUserRoleLambdaQueryWrapper);
        return Results.success("已成功删除该用户的此角色！");
    }

    @Override
    @Transactional
    public Results sysUserRoleUpdate(SysUserRoleParam sysUserRoleParam) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUserName,sysUserRoleParam.getUserName()).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        if (sysUser == null) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),ErrorCode.USER_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,sysUserRoleParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode() ,ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        sysUserRoleWrapper.eq(SysUserRole::getUserId,sysUser.getUserId()).last("limit 1");
        SysUserRole sysUserRole1 = sysUserRoleMapper.selectOne(sysUserRoleWrapper);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserRoleDeleted(0);
        sysUserRole.setUserRoleId(sysUserRole1.getUserRoleId());
        sysUserRole.setRoleId(role.getRoleId());
        sysUserRole.setUserId(sysUser.getUserId());
        LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserRoleLambdaQueryWrapper.eq(SysUserRole::getUserRoleId,sysUserRole.getUserRoleId());
        sysUserRoleMapper.update(sysUserRole,sysUserRoleLambdaQueryWrapper);
        return Results.success("修改用户的角色成功！");
    }

    @Override
    @Transactional
    public Results sysUserRoleSelect(String userName) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUserName,userName).last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);
        if (sysUser == null) {
            return Results.fail(ErrorCode.USER_NOT_EXIST.getCode(),ErrorCode.USER_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<SysUserRole> sysUserRoleWrapper = new LambdaQueryWrapper<>();
        sysUserRoleWrapper.eq(SysUserRole::getUserId,sysUser.getUserId());
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(sysUserRoleWrapper);
        List<String> list = new ArrayList<>();
        SysUserRoleVo sysUserRoleVo = new SysUserRoleVo();
        sysUserRoleVo.setUserName(userName);
        for (SysUserRole sysUserRole : sysUserRoles) {
            LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleLambdaQueryWrapper.eq(Role::getRoleId,sysUserRole.getRoleId()).last("limit 1");
            Role role = roleMapper.selectOne(roleLambdaQueryWrapper);
            list.add(role.getRoleName());
        }
        sysUserRoleVo.setRoleName(list);
        return Results.success(list);
    }
}
